﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Util;


public class FallingPlatformCtr : PlatformCtl
{

    public float waitForFall = 2f;

    private bool stopMoveUp;

    private Rigidbody2D rb;
    private Collider2D coll;
    private Animator anim;

    private int fallingAnimID = Animator.StringToHash("falling");

    
    public override void Start()
    {
        base.Start();

        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<Collider2D>();
        anim = GetComponent<Animator>();
    }


    public override void Update()
    {
        if (!stopMoveUp)
        {
            base.Update();
        }

        
    }

    private void OnCollisionEnter2D(Collision2D collision)
    {
        if (Utils.IsPlayer(collision))
        {
            Invoke(nameof(PlatformFall), waitForFall);
        }
    }

    void PlatformFall()
    {
        stopMoveUp = true;

        anim.SetTrigger(fallingAnimID);

        coll.enabled = false;
        rb.bodyType = RigidbodyType2D.Dynamic;
        rb.mass *= 5;

        Invoke(nameof(WillDestroy), 2);
    }

    void WillDestroy()
    {
        Destroy(gameObject);
    }
}
